package com.coal.exam.web;

import com.alibaba.fastjson.JSON;
import com.coal.exam.bean.C;
import com.coal.exam.bean.ExcelUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

@Service
public class ExerciseService {
    private String URL = "http://lxpt.nmgmt.gov.cn/fore/exercise/getExercise?qNum=";
    @Value("${template.path}")
    private String templatePath;
    @Value("${save.path}")
    private String savePath;

    public void get() throws Exception{
        List<C> list = Lists.newArrayList();
        for(int i = 10;i<650;i=i+10){
            if(i==650){
                i=i-1;
            }
            String url = URL + i;
            String http = http(url);
            List<C> parse = parse(http);
            list.addAll(parse);
        }
        Map<String,String> dataMapping = Maps.newHashMap();
        dataMapping.put("题目","tq_name");
        dataMapping.put("选项","xx");
        dataMapping.put("答案","bz_answer");
        try {
            FileInputStream fileInputStream = new FileInputStream(templatePath);
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            ExcelUtil<C> excelUtil = new ExcelUtil<>(null,C.class,dataMapping);
            excelUtil.export(list,workbook);
            FileOutputStream fileOutputStream = new FileOutputStream(savePath);
            workbook.write(fileOutputStream);
            workbook.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        System.out.println(list);
    }

    private String http(String url){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        /* 这个对象有add()方法，可往请求头存入信息 */
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("Cookie","JSESSIONID=292AB16752FC52D98E18FF63E668DE48");
        /* 解决中文乱码的关键 , 还有更深层次的问题 关系到 StringHttpMessageConverter，先占位，以后补全*/
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        /* body是Http消息体例如json串 */
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        String body = exchange.getBody();
        String replace = body.replace("\\", "");
        return replace.substring(1,replace.length()-1);
    }

    private List<C> parse(String content){
        System.out.println("待格式化JSON："+content);
//        String content = "{\"questions\":[{\"tq_id\":\"F1EA7227A87DF878DCEDD10EE5DE4A5DF89BBC4E5C4CE7989E5581886C53DCAF8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"劳动防护用品是用来保护从业人员作业安全和健康的预防性辅助装备。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"B12E2CF4B4015B459336A5823F17EAB5F4ED3345455BC401F47E3DB42F4A66DD8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"矿井瓦斯等级是矿井瓦斯涌出量大小的基本标志。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"0F42C98D1A49A791072A26736E02A314DD795A719F9C5F61FB4E6A70378DDB108C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"若在下邻近层掘汇集瓦斯巷道进行抽采,即为下邻近层瓦斯抽采。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"E7280F95481B69D6F4416BBC35EE8E532274B22702F3850EA7FC92C80E0825B88C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"入井人员乘罐时,要服从井口把钩人员指挥,自觉遵守入井检身制度和出入井人员清点制度。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"6202B54241A12E50F44E8449E1289AD0273040C690ADCC02C6CA1D352799E2708C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"从岩体或煤体裂隙和孔洞中释放出瓦斯的现象称为瓦斯喷出。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"C2B239F269A3BC4E501DD51B2BE55E3D961785B76DE873CD84679EB9A0DC45FD8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"制定专项措施后,可在停风或瓦斯超限的区域作业。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"316D1E1AFCC7AED0E97ADDF2021E269A0E9CE016996DD1C2E1AE1233AE5950E18C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"当混合气体中瓦斯浓度大于16%时,仍然会燃烧和爆炸。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"716629D3B2FAB86A6C066725AF3A4F9C04D19D65FC58650B60CF0AB1005B32CD8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"漏电是由于带电导体的绝缘性能下降或损坏、导体间电气间隙和爬电距离变小、带电导体接壳或接地造成的。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"7A7AC3AD24647FEE326B5183929F36E0703C8F055C325599B57209A7314FB5898C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"井下电气设备检修必须严格执行相关规程规定和工作程序,并悬挂标志牌。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"75A0CB8EE51B61A885A8EFC10472F1C2F6B9E4739BD82E4DA8E562B361E2C4758C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"用于煤矿井下的电气设备一定是防爆型电气设备。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"8DF1CFA9DBA33F0F8541F4CE4BF4A8E63BCFC11401671FE75873D899B7DDF2148C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"邻近层的瓦斯抽采,可采用倾斜高抽巷,沿走向间距一般取150~200m。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"FECE5773AF46960AB720F94D914CC78BF778C5DD439815F9A55EE9ADFAEBC57A8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"采煤工作面上隅角、顶板冒落空洞等处容易积聚瓦斯。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"517E857905A2B37D18DD66A50BF47EC211AF6103A1F28B97EC5CB9FC8DCACD998C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"瓦斯爆炸时产生的冲击有正向(直接)冲击和反向冲击。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"02ACC7708CEFE84EC03611CC8494BF8434BF15FB68D039B83081A67C2A091FE18C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"煤矿井下永久性避难硐室是供矿工在劳动时休息的设施。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"91176EA925CEC9616845DF5B3BEFF957A3B00A37BA1F9D4A20B2AB5C4D26ED698C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"对瓦斯抽采泵进行停泵操作时,要先停水,再停泵,最后关进出气端阀门。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"1F39146BEF05EBF78725C1D116D6284EEA80CACA49751447411EFA7A48FAEDAB8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"在生产作业中违反有关安全管理规定,因而发生重大伤亡事故或者造成其他严重后果的,处3年以下有期徒刑或者拘役;情节特别恶劣的,处3年以上7年以下有期徒刑。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"E8EBE6FAB8B0529A092FA6D0C21A43B4DADB4D053ABE0BE7603D25745C3A6D0C8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"严格执行敲帮问顶制度,开工前班组长必须对工作面安全情况进行全面检查,确认无安全隐患后,方准人员进入工作面。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"DB008B55C4E7FAADFACF57BAE33A8636416CD094584CDAF92010A52D78EEF4F88C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"煤矿使用的涉及安全生产的劳动保护用品,必须符合国家标准或者行业标准,必须取得煤矿矿用产品安全标志。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"D1620D4B6F81C856DC86A1F50835A321917E5D93E2D4642D123B4AEAE4526B5B8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"瓦斯抽采泵电动机出现发热现象,要用水对其进行冷却。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"8E0C9B262373CA4B5655DB270BED7F4F5025E8E3BFE352FE394DB0ADBC77B0A48C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"矿井相对瓦斯涌出量是指矿井正常生产条件下,月平均日产一吨煤所涌出的瓦斯量。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"31310323C9FFDA9C33B919A5243BE8D0F54F844E6A9D0037122A274E5702C18B8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"本质安全型电气设备,在正常工作状态下产生的火花或热效应不能点燃爆炸性混合物;在故障状态下产生的火花或热效应会点燃爆炸性混合物。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"A547599A525203A495DC5F24F904660AB60148B1C3C95CAFFF15B7DF914A01C88C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"井下急救互救中必须遵循“先复苏、后搬运,先止血、后搬运,先固定、后搬运”的原则。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"91DBA4AF35B2A7A05D9C5FCE505BE63A1D012C91B6DED91D238EFD1F5F1C44C48C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"在采煤工作面回采工序中,爆破或采煤机割煤时的瓦斯涌出量最高。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"FF26064880684551C48741B2CB4604AD72615336E3FC7AC623096C26AEBDB5708C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"瓦斯抽采工要确保抽采管路无破损、无泄漏、无积水。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"AB68AEDEC006C35D2185BE5A0E511BCDB31EBF4084BD985E5E87B35FEAC33DAC8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"矽肺是一种致残性职业病,主要是作业人员吸入大量的含有游离二氧化硅的岩尘所致。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"C65527C3D90E6CAF75F2C6824D6633F7E5D495D2FDF73944B6EFA8590D0321FA8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"在发生安全事故后,从业人员有获得及时抢救和医疗救治并获得工伤保险赔偿的权利。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"E0CA54A12C34DB57AC3D255CC79E97ABBA1EBDCD76FC707DCA3E2C698884623E8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"煤矿企业要保证“安全第一,预防为主,综合治理”方针的具体落实,必须严格执行《煤矿安全规程》等相关规定。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"21854A2C599531666F20EA8E81C94A8A544ACDC340785CEAED2DBE5BCAC25EFE8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"煤矿特种作业人员具有丰富的现场工作经验,就可以不参加培训。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"335AB06046076778666A600543B071DD8CEA1F53112EB84F8442D6B124C0963A8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"对于脊柱损伤人员,严禁让其坐起、站立和行走。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"FA9C27CB3B31E7FC798F8535FBCC8434CAF1CC8126266554574BC44AAB47C6248C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"特种作业人员应当经社区或者县级以上医疗机构体检健康合格,并无妨碍从事相应特种作业的器质性疾病和生理缺陷。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"C74FCA4B9EEA36EEC7B519FEB6CB2C0CAD7F355E8EDC4998B406F77EED117E928C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"炸药爆炸产生的火焰持续的时间只要在瓦斯感应期以内,就不会引起瓦斯爆炸。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"561A598E060E1972E38B8F442471C3C8DFA525554EDB56F395D95DC19ADD87648C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"瓦斯抽采钻孔使用导向钻头和导向钻具,不能定向钻孔施工。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"},{\"tq_id\":\"BC99E19D6FF276053FC1B4843CB76C7BC74E03C21A5CB159EC411AF1FC9BCC1F8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"开采煤层瓦斯未卸压钻孔排放时,钻孔的有效排放半径是指在规定的排放时间内,在有效排放半径范围内的瓦斯压力或瓦斯含量降到安全允许值。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"508DD3F692D07EEE3A9AA055A2F154823DF4A06CFA7B22330795A1AF77A6FC3C8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"在井下拆卸矿灯会产生电火花,可引起瓦斯和煤尘爆炸事故。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"A\"},{\"tq_id\":\"FFA2FBF79903FEF9445D28F9D0C91D4058CDEF8AE9A9089AE1AF2606E4C9D35D8C8CC192139417E3\",\"tq_type\":\"3\",\"tq_name\":\"抽采容易自燃和自燃煤层的采空区瓦斯时,必须经常检查一氧化碳浓度和气体温度参数的变化,发现有自然发火征兆时,应予上报,不必急于处理。()\",\"xx\":[\"对\",\"错\"],\"da_jx\":\"null\",\"bz_answer\":\"B\"}],\"begin\":\"95\",\"end\":\"130\",\"num\":\"100\",\"allnum\":\"644\"}";
        List<C> list = Lists.newArrayList();
        try {
            JSONObject obj = new JSONObject(content);
            JSONArray obj2 = obj.getJSONArray("questions");
            for(int i = 0;i<obj2.length();i++){
                Object o = obj2.get(i);
                C c = JSON.parseObject(o.toString(), C.class);
//                C c = JsonMapper.nonEmptyMapper().fromJson(o.toString(), C.class);
                list.add(c);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println("返回题目数："+list.size());
        return list;
    }
}
